package com.elvish.efast.api.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.elvish.efast.anno.BuildTree;
import com.elvish.efast.common.consts.Const;
import com.elvish.efast.common.entity.Result;
import com.elvish.efast.common.entity.TreeData;
import com.elvish.efast.common.enums.ResultCode;
import com.elvish.efast.common.utils.CommonUtil;
import com.elvish.efast.common.utils.ResultUtil;
import com.elvish.efast.model.entity.Org;
import com.elvish.efast.service.OrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (TOrg)表控制层
 *
 * @author makejava
 * @since 2018-11-14 11:04:12
 */
@RestController
@RequestMapping("/org")
@Slf4j
public class OrgApi {
    /**
     * 服务对象
     */
    @Reference
    private OrgService tOrgService;

    /**
     * 查询列表
     *
     * @param org 查询参数
     * @return 对象列表
     */
    @GetMapping("/queryAll")
    public Result queryAll(Org org) {
        return ResultUtil.success(tOrgService.queryAll(org));
    }

    /**
     * 查询所有组织机构
     *
     * @return 所有组织机构数据
     */
    @GetMapping("/getTreeData")
    @BuildTree
    public List<TreeData> getTreeData() {
        return tOrgService.getTreeData();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Org selectOne(String id) {
        return this.tOrgService.queryById(id);
    }

    /**
     * 操作组织机构
     *
     * @param oper 操作符
     * @param org  菜单对象
     * @return 响应消息
     */
    @PostMapping("/editOrg")
    public Result editOrg(int oper, @RequestBody Org org) {
        Result result = null;
        try {
            if (oper == Const.OperationType.ADD) {
                org.setId(CommonUtil.uuid());
                this.tOrgService.insert(org);
            } else if (oper == Const.OperationType.UPDATE) {
                this.tOrgService.update(org);
            } else if (oper == Const.OperationType.DELETE) {
                this.tOrgService.deleteById(org);
            }
            result = ResultUtil.success();
        } catch (Exception e) {
            log.error("editMenu error", e);
            result = ResultUtil.fail(ResultCode.ERROR);
        }
        return result;
    }

}